package org.arnetminer.personalized.util;

/**


 */
public class Page {

	private int currentPage = 0;
	private int totalPages;
	private int pageRecorders;
	private int pageStartRow;
	private int pageEndRow;
	private int totalRows;
	private boolean hasPreviousPage;
	private boolean hasNextPage;

	public Page(int totalRows, int pageRecorders) {
		this.totalRows = totalRows;
		this.currentPage = 1;
		this.hasPreviousPage = false;
		this.pageRecorders = pageRecorders;
		if (this.totalRows % this.pageRecorders == 0) {
			this.totalPages = this.totalRows / this.pageRecorders;
		} else {
			this.totalPages = this.totalRows / this.pageRecorders + 1;
		}
		if (this.currentPage >= this.totalPages) {
			this.hasNextPage = false;
		} else {
			this.hasNextPage = true;
		}
		if (this.totalRows < this.pageRecorders) {
			this.pageStartRow = 0;
			this.pageEndRow = this.totalRows;

		} else {
			this.pageStartRow = 0;
			this.pageEndRow = this.pageRecorders;

		}
	}

	public String getCurrentPage() {
		return this.toString(currentPage);
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageRecorders() {
		return pageRecorders;
	}

	public void setPageRecorders(int pageRecorders) {
		this.pageRecorders = pageRecorders;
	}

	public String getTotalPages() {
		return this.toString(totalPages);
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public String getTotalRows() {
		return this.toString(totalRows);
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}

	public void setHasPreviousPage(boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}

	public String toString(int temp) {
		return Integer.toString(temp);
	}

	public void getFirstPage() {
		this.currentPage = 1;
		this.hasPreviousPage = false;
		if (this.currentPage >= this.totalPages) {
			this.hasNextPage = false;
		} else {
			this.hasNextPage = true;
		}
	}

	public void getNextPage() {
		this.currentPage = this.currentPage + 1;
		if (this.currentPage - 1 > 0) {
			this.hasPreviousPage = true;
		} else {
			this.hasPreviousPage = false;
		}
		if (this.currentPage >= this.totalPages) {
			this.hasNextPage = false;
		} else {
			this.hasNextPage = true;
		}
	}

	public void getPrevioursPage() {
		this.currentPage = this.currentPage - 1;
		if (this.currentPage == 0) {
			this.currentPage = 1;
		}
		if (this.currentPage >= this.totalPages) {
			this.hasNextPage = false;
		} else {
			this.hasNextPage = true;
		}
		if (this.currentPage - 1 > 0) {
			this.hasPreviousPage = true;
		} else {
			this.hasPreviousPage = false;
		}
	}

	public void getLastPage() {
		this.currentPage = this.totalPages;
		if (this.currentPage == 0) {
			this.currentPage = 1;
		}
		this.hasNextPage = false;
		if (this.currentPage - 1 > 0) {
			this.hasPreviousPage = true;
		} else {
			this.hasPreviousPage = false;
		}
	}

	public void setGotoPage(int gotoPage) {
		this.currentPage = gotoPage;
		this.hasNextPage = true;
		this.hasPreviousPage = true;
		if (this.currentPage <= 1) {
			this.currentPage = 1;
			this.hasNextPage = true;
			this.hasPreviousPage = false;
		}
		if (this.currentPage > this.totalPages) {
			this.currentPage = this.totalPages;
			this.hasNextPage = false;
			this.hasPreviousPage = true;
		}
	}

	public Page next() {
		if (this.currentPage * this.pageRecorders < this.totalRows) {
			this.pageEndRow = this.currentPage * this.pageRecorders;
			this.pageStartRow = this.pageEndRow - this.pageRecorders;
		} else {
			this.pageEndRow = this.totalRows;
			this.pageStartRow = this.pageRecorders * (this.totalPages - 1);
		}
		return this;
	}

	public int getPageStartRow() {
		return pageStartRow;
	}

	public void setPageStartRow(int pageStartRow) {
		this.pageStartRow = pageStartRow;
	}

	public int getPageEndRow() {
		return pageEndRow;
	}

	public void setPageEndRow(int pageEndRow) {
		this.pageEndRow = pageEndRow;
	}

}
